var app = getApp();
let innerAudioContext = wx.createInnerAudioContext(); //创建音频实例
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgdefalut: app.myUlife.ulifeUrl + 'wx/topimg2.jpg',//默认图片
    tjimagedefault: app.myUlife.ulifeUrl + 'testimage/timg1.jpg',//默认

    task: {
      page: 1,
      limit: 20,
      totalpage: 0,
      datas: []
    },
    totalnum:0,
    user_type: '1',//1学生 2教师

    playStatus:true,
    slider_value:0,
    formatedPlayTime: "00:00",
    homework: [],

    startgray: '../../../images/score_stars_icon_gray.png',//星级灰色
    startyellow: '../../../images/score_stars_icon_yellow.png',//星级黄色
    starsy: 0,
    starsn: 5,
    keyword:'',

    prompt: {
      hidden: !0,
      icon: '../../../assets/images/iconfont-order-default.png',
      title: '提示',
      text: '当前没有任何的任务',
      gurl: '../../../pages/home/home',
      gtype: "switchTab"
    },
    


    //作业诗词录音播放
    playStatus: true, //未播放的图片
    slider_value: 0,//设置初始滑块位置为0
    formatedPlayTime: '00:00',
   
    topimage: app.myUlife.ulifeUrl + 'wx/topimg2.jpg',//默认图片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.gettopImage();//查询顶部图片
  },

  gettopImage(){
    var that = this;
    app.wxget({
      url: 'scdh/getPoetryPic.html',
      success: function (res) {
        var imgurl = res.data.PICURL;
        if (imgurl){
          that.setData({
            topimage: imgurl,
          })
        }
      }
    })
  },

  getMytask: function (e) {
    var that = this;
    var usid = wx.getStorageSync('usid');
    var page = that.data.task.page;
    var limit = that.data.task.limit;
    var totalpage = that.data.task.totalpage;
    if (page != 1) {
      if (page > totalpage) {
        return;
      }
    } 
    var nj = wx.getStorageSync('nj');
    var bj = wx.getStorageSync('bj');
    var keyword=this.data.keyword;
    var user_type = wx.getStorageSync('user_type');
    app.wxget({
      url: 'poetryread/getPoetryread.html',
      data: {
        usid: usid,
        nj: nj,
        bj: bj,
        keyword: keyword,
        user_type:user_type,
        page: page,
        limit: limit,
      },
      success: function (res) {
        var datas = res.data.list;
        var totalpage = res.data.totalpage;
        var odatas = that.data.task.datas;
        for (var i = 0; i < datas.length; i++) {
          odatas.push(datas[i]);
        }
        var task = that.data.task;
        task.datas = odatas;
        task.totalpage = totalpage;
        that.setData({
          task: task,
          totalnum: res.data.totalnum,
          'prompt.hidden': odatas.length
        })
      }
    })
  },

  getHomeWork: function (e) {
    //查询当天的作业检查
    var that = this;
    var dlzh = wx.getStorageSync('dlzh');
    app.wxget({
      url: 'poetryread/getHomeWork.html',
      data: {
        dlzh: dlzh
      },
      success: function (res) {
        var hworks = res.data;
        var homework=[];
        for (var i = 0; i < hworks.length; i++) {
          var ws={
            USID: hworks[i].USID,
            USERNAME: hworks[i].USERNAME,
            READID: hworks[i].READID,
            YP: hworks[i].YP,
            PUBILSHTIME: hworks[i].PUBILSHTIME,
            PSTATR: hworks[i].PSTATR,
            PUSID: hworks[i].PUSID,
            PUSERNAME: hworks[i].PUSERNAME,
            PNAME: hworks[i].PNAME,
            AUTHOR: hworks[i].AUTHOR,
            NJ: hworks[i].NJ,
            BJ: hworks[i].BJ,
            FM: hworks[i].FM,
            playStatus: true, //未播放的图片
            slider_value: 0,//设置初始滑块位置为0
            formatedPlayTime: '00:00', 
          }
          homework.push(ws);
        }

        that.setData({
          homework: homework,
        })
      }
    })

  },
  


  errorFunction: function (e) {
    if (e.type == "error") {
      this.setData({
        topimage: this.data.imgdefalut
      })
    }
  },


  errormoretj: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.errorimg
      var task = this.data.task;
      var datas = task.datas;  　　　　　　　//将图片列表数据绑定到变量
      datas[errorImgIndex].FM = this.data.tjimagedefault; //错误图片替换为默认图片
      this.setData({
        task: task
      })
    }
  },


  //输入关键字
  keyinput: function(e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  //搜索
  searchmywork(){
    this.setData({
      task: {
        page: 1,
        limit: 20,
        totalpage: 0,
        datas: []
      }
    })
    this.totalnum=0;
    this.getMytask();//查询我的任务
  },

  toTaskDetail(e){
    var that = this;
    var readid = e.currentTarget.dataset.readid;
    var logs = e.currentTarget.dataset.logs;
    var bstate = e.currentTarget.dataset.bstate;
    var user_type=that.data.user_type;
    if (user_type == "1") {//1学生 2教师
      wx.navigateTo({
        url: '../../task/poetryDetail/poetryDetail?readid=' + readid
      })
    }else{
      if (bstate==0){//未发布
        wx.navigateTo({
          url: '../../task/fatask/fatask?readid=' + readid
        })
      }else{
        wx.navigateTo({
          url: '../../task/poetryDetail/poetryDetail?readid=' + readid
        })
      }
    }
  },





  //播放start

  //开始播放
  ksbf: function (e) {
    let that = this
    var index = e.currentTarget.dataset.index;
    var musicUrl =  e.currentTarget.dataset.yp; //音频url

    var homework = that.data.homework;
    for (var i = 0; i < homework.length; i++) {
      if(i==index){
        homework[index].playStatus=false
      }else{
        homework[i].formatedPlayTime="00:00";
        homework[i].playStatus=true;
        homework[i].slider_value="0";
      }
    }

    this.setData({
      homework:homework
    })

    innerAudioContext.src = musicUrl;
    innerAudioContext.autoplay = true
    innerAudioContext.play();
    innerAudioContext.onCanplay((res) => {
      var duration = innerAudioContext.duration;
      innerAudioContext.onTimeUpdate((res) => {
        var per = (innerAudioContext.currentTime / innerAudioContext.duration) * 100;
       
        homework[index].formatedPlayTime = this.formatTime(innerAudioContext.currentTime);
        homework[index].slider_value = per;
        that.setData({
          homework: homework
        })
    

        if (innerAudioContext.duration.toFixed(2) - innerAudioContext.currentTime.toFixed(2) <= 0) {
          that.setStopState(index)
        }
        innerAudioContext.onEnded(() => {
          that.setStopState(index)
        })
      })
    })
  },

  setStopState: function (index) {
    var homework = this.data.homework;
    homework[index].formatedPlayTime = '00:00';
    homework[index].playStatus = true;
    this.setData({
      homework: homework
    })

    innerAudioContext.seek(0);
    innerAudioContext.stop();
  },


  //结束播放
  canel_handover(e) {
    innerAudioContext.pause(); //语音暂停
    var index = e.currentTarget.dataset.index;
    var homework = this.data.homework;
    homework[index].playStatus = true;
    this.setData({
      homework: homework
    })
  },


  listenerSlider: function (e) {
    var per = e.detail.value;
    var currentTime = (per / 100) * innerAudioContext.duration;
    innerAudioContext.seek(currentTime);//通过滑块控制音频进度
    this.formatTime(currentTime)
  },


  formatTime: (time) => {
    time = Math.floor(time);
    var m = Math.floor(time / 60).toString();
    m = m.length < 2 ? '0' + m : m;
    var s = (time - parseInt(m) * 60).toString();
    s = s.length < 2 ? '0' + s : s;
    return m + ':' + s;
  },
  //播放end




  //点击星星打分
  getstar(e) {
    var readid = e.currentTarget.dataset.readid;
    var pstatr = e.currentTarget.dataset.star;
    var pusid = wx.getStorageSync('usid');
    var pusername = wx.getStorageSync('name');
    var index = e.currentTarget.dataset.index;
    var usid = e.currentTarget.dataset.usid;
    var that = this;
    app.wxget({
      url: 'poetryread/setPoetryreadlogStar.html',
      data: {
        readid: readid,
        pstatr: pstatr,
        pusid: pusid,
        pusername: pusername,
        usid:usid
      },
      success: function (res) {
        if (res.data.result) {
          var homework = that.data.homework;
          homework[index].PSTATR = pstatr;
          that.setData({
            homework: homework
          })
          
        } else {
          wx.showModal({
            title: '友情提示',
            content: '打分失败!',
            showCancel: !1,
          })
        }
      }
    })
  },



  gotoScdh(e) {
    var that = this;
    app.wxget({
      url: 'wscdh/getScdhNow.html',
      success: function (res) {
        if (!res.data.PID) {
          //查询我最近是否参加过诗词大会
          that.myLastScdhInfo()
        }else{
          that.myScdhInfo(res.data.PID)
        } 
      }
    })
  },

  //查询我是否参加过诗词大会
  myScdhInfo(pid) {
    var that = this;
    var usid = wx.getStorageSync('usid');
    app.wxget({
      url: 'wscdh/getmyScdhInfo.html',
      data: {
        bid: pid,
        usid: usid
      },
      success: function (res) {
        if (res.data.length>0) {
          //我参加过诗词大会 进入答题成功页面
          wx.navigateTo({
            url: '../../../pages/scdhcg/scdhcg?bid=' + pid
          })
        } else {
           //我没有参加过诗词大会 进入答题页面
          wx.navigateTo({
            url: '../../../pages/scdh/scdh?pid=' + pid
          })
        }
      }
    })
  },




  //查询我最近是否参加过诗词大会
  myLastScdhInfo() {
    var that = this;
    var usid = wx.getStorageSync('usid');
    app.wxget({
      url: 'wscdh/myLastScdhInfo.html',
      data: {
        usid: usid
      },
      success: function (res) {
        if (res.data.length > 0) {
          //我参加过诗词大会 进入答题成功页面
          wx.navigateTo({
            url: '../../../pages/scdhcg/scdhcg?bid=' + res.data[0].BID
          })
        } else {
          //我没有参加过诗词大会  也没有开启的诗词大会  所以提示
          wx.showModal({
            title: '友情提示',
            content: '没有开启任何诗词大会!',
            showCancel: !1,
          })
        }
      }
    })
  },

  onShow: function (options) {
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../../pages/login/login'
      })
      return;
    }
    var user_type = wx.getStorageSync('user_type');
    this.setData({
      user_type: user_type
    })


    this.setData({
      task: {
        page: 1,
        limit: 20,
        totalpage: 0,
        datas: []
      },
      totalnum: 0,
      homework: [],
    })
    this.getMytask();//查询我的任务
    this.getHomeWork();//查询作业检查
  },

})